const app = getApp ();
import {
  getPlanInfoApi,
  deleteTeamApi,
  returnTeamApi,
  editPlanApi,
} from '../../api/app';
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    show: false,
    actions: [
      {
        name: '创建事项',
      },
      {
        name: '加入事项',
      },
    ],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getPlanInfo()
    this.setData ({
      openid: wx.getStorageSync ('openId'),
    });
  },
  onShow: function (options) {
    // this.getPlanInfo ();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来加入协同小程序吧~',
    };
  },
  toCreact(){
    
  },
  // 获取当前团队
  async getPlanInfo () {
    const openid = wx.getStorageSync ('openId');
    try {
      // read或空表示阅读打卡，sport表示运动打卡
      const res = await getPlanInfoApi ({
        openid,
      });
      if (res.code == 200 && res.result) {
        this.setData ({
          team: res.result,
          isCaptain: res.result.uid == openid,
        });
      } else {
        this.setData ({
          team: null,
        });
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 创建团队
  toClockinPlan () {
    wx.navigateTo ({
      url: '/subPages/clockin/create-book/index',
    });
  },
  // 加入团队
  toClockin () {
    wx.navigateTo ({
      url: '/subPages/joinTeam/index',
    });
  },
  // 复制
  copy () {
    console.log (this.data.team.code);
    wx.setClipboardData ({
      data: this.data.team.code || '',
      success (res) {
        wx.showToast ({
          title: '复制成功',
          icon: 'none',
        });
        // wx.getClipboardData({
        //   success (res) {
        //     console.log(res.data) // data
        //   }
        // })
      },
    });
  },
  onChange (e) {
    this.setData ({
      checked: e.detail,
    });
    console.log (e);
  },
  async confirmEdit () {
    try {
      if (this.data.currenteInput.id) {
        const res = await editPlanApi (this.data.currenteInput);
        if (res.code == 200) {
          this.setData ({
            currenteInput: {
              id: '',
            },
          });
        }
        wx.showToast ({
          title: res.msg,
          icon: 'none',
        });
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 监听输入
  bindinput (e) {
    console.log (e);
    const {index} = e.currentTarget.dataset;
    const {value} = e.detail;
    this.setData ({
      currenteInput: {
        id: index,
        planPerPlan: value,
      },
    });
  },
  bindfocus (e) {
    const {index} = e.currentTarget.dataset;
    const {value} = e.detail;
    this.setData ({
      currenteInput: {
        id: index,
        planPerPlan: value,
      },
    });
  },
  handleReturn () {
    wx.showModal ({
      title: '提示',
      content: '你确定要退出当前团队？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.backTeam (wx.getStorageSync ('openId'));
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  handleDelete () {
    wx.showModal ({
      title: '提示',
      content: '你确定要解散当前团队？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.deleteTeam ();
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  handleRemove (e) {
    wx.showModal ({
      title: '提示',
      content: '你确定要移除该成员？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        const {index} = e.currentTarget.dataset;
        if (result.confirm) {
          this.backTeam (index);
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  // 退出团队
  async backTeam (id) {
    try {
      const res = await returnTeamApi ({
        openID: id,
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '退出成功！',
          icon: 'none',
        });
        setTimeout (() => {
          this.getPlanInfo ();
        }, 1500);
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 删除团队
  async deleteTeam () {
    try {
      const res = await deleteTeamApi ({
        openID: wx.getStorageSync ('openId'),
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '解散成功！',
          icon: 'none',
        });
        setTimeout (() => {
          this.getPlanInfo ();
        }, 1500);
      }
    } catch (error) {
      console.log (error);
    }
  },
});
